//
//  UIBubbleTableViewCell.m
//
//  Created by Alex Barinov
//  StexGroup, LLC
//  http://www.stexgroup.com
//
//  Project home page: http://alexbarinov.github.com/UIBubbleTableView/
//
//  This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
//  To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
//


#import "UIBubbleTableViewCell.h"
#import "NSBubbleData.h"

@interface UIBubbleTableViewCell ()
- (void) setupInternalData;
@end

@implementation UIBubbleTableViewCell

@synthesize dataInternal = _dataInternal;

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
	[self setupInternalData];
}

- (void)awakeFromNib {
    self.contentView.backgroundColor = APP_OBJECT_GRAY_COLOR;
    avatarsImageView.backgroundColor = [UIColor clearColor];
    avatarsImageView.layer.masksToBounds = YES;
    [avatarsImageView.layer setCornerRadius:22.5];
    [avatarsImageView.layer setBorderWidth:2];
    [avatarsImageView.layer setBorderColor:[UIColor whiteColor].CGColor];
    
    headerLabel.backgroundColor = APP_KINDER_TIME_LABEL_BGCOLOR;
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.layer.masksToBounds = YES;
    [headerLabel.layer setCornerRadius:7];
    headerLabel.font = [UIFont systemFontOfSize:12];
}

- (void) dealloc
{
    [_dataInternal release];
	_dataInternal = nil;
    [super dealloc];
}


- (void)setDataInternal:(NSBubbleDataInternal *)value
{
	[value retain];
	[_dataInternal release];
	_dataInternal = value;
	[self setupInternalData];
}

- (void) setupInternalData
{
    if (self.dataInternal.header)
    {
        headerLabel.hidden = NO;
        headerLabel.text = self.dataInternal.header;
    }
    else
    {
        headerLabel.hidden = YES;
    }
    [headerLabel sizeToFit];
    headerLabel.frame = CGRectMake((SCREEN_WIDTH-CGRectGetWidth(headerLabel.frame))/2-10, (35-CGRectGetHeight(headerLabel.frame))/2, CGRectGetWidth(headerLabel.frame)+20, CGRectGetHeight(headerLabel.frame)); //时间Text
    
    NSBubbleType type = self.dataInternal.data.type;
    float x = (type == BubbleTypeSomeoneElse) ? 75 : self.frame.size.width - 20 - self.dataInternal.labelSize.width - 50;
    float y = 5 + (self.dataInternal.header ? 30 : 0);
    contentLabel.font = [UIFont systemFontOfSize:16];
    contentLabel.frame = CGRectMake(x, y+5, self.dataInternal.labelSize.width, self.dataInternal.labelSize.height);
    contentLabel.text = self.dataInternal.data.text;
    [contentLabel sizeToFit]; //内容Text
    
    if (type == BubbleTypeSomeoneElse)
    {
        bubbleImage.image = [[UIImage imageNamed:@"zuobian.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];
        bubbleImage.frame = CGRectMake(55, y - 4, self.dataInternal.labelSize.width + 35, self.dataInternal.labelSize.height + 20); //背景图片
        
        contentLabel.textColor = [UIColor whiteColor]; //左边内容文字颜色
        avatarsImageView.frame = CGRectMake(5, y - 4*2 + 2, 45, 45); // 头像
    }
    else {
        bubbleImage.image = [[UIImage imageNamed:@"youbian.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:14];
        bubbleImage.frame = CGRectMake(x - 13, y - 4, self.dataInternal.labelSize.width + 30, self.dataInternal.labelSize.height + 20); //背景图片
        contentLabel.textColor = [UIColor blackColor]; //右边内容文字颜色
        avatarsImageView.frame = CGRectMake(270, y-4*2+self.dataInternal.labelSize.height-20 + 2, 45, 45); //头像
    }
}

@end
